package com.lh.learning.mq.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @author luohuan
 * @date 2020/6/18
 **/
@Configuration
public class RabbitMqConfig {

    @Bean
    public Queue messageQueue() {
        return QueueBuilder.durable("message").build();
    }

    @Bean
    public Queue delayedMessageQueue() {
        return QueueBuilder.durable("delayed.message").build();
    }

    @Bean
    public Queue replyMessageQueue() {
        return QueueBuilder.durable("reply.message").build();
    }

    @Bean
    public Exchange messageExchange(){
        return ExchangeBuilder.directExchange("direct.message").build();
    }

    @Bean
    public Exchange messageFanoutExchange() {
        return ExchangeBuilder.fanoutExchange("fanout.message").build();
    }

    @Bean
    public Exchange messageDelayedExchange() {
        Map<String, Object> args = new HashMap<>();
        args.put("x-delayed-type", "direct");
        return new CustomExchange("delayed.message", "x-delayed-message", true, false, args);
    }

    @Bean
    public Binding delayedMessageBinding(){
        return BindingBuilder.bind(delayedMessageQueue()).to(messageDelayedExchange()).with("").noargs();
    }

    @Bean
    public Binding messageBinding(){
        return BindingBuilder.bind(messageQueue()).to(messageExchange()).with("message.routing").noargs();
    }

    @Bean
    public Binding messageFanoutBinding() {
        return BindingBuilder.bind(messageQueue()).to(messageFanoutExchange()).with("fanout.routing").noargs();
    }

    @Bean
    public Binding replyMessageBinding(){
        return BindingBuilder.bind(replyMessageQueue()).to(messageExchange()).with("reply.routing").noargs();
    }
}
